package triannon.client.util;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Vector;

import javax.imageio.ImageIO;

import triannon.client.sprites.SpriteType;

public class ResourceLocator {
	private static final ClassLoader CLASS_LOADER = ResourceLocator.class
			.getClassLoader();

	private static final Vector<String> RESOURCE_DIRS = new Vector<String>();

	public static void addResourceDir(String dir) {
		RESOURCE_DIRS.add(dir);
	}

	public static URL getResource(String filename) {
		URL url = null;

		for (String dir : RESOURCE_DIRS) {
			url = CLASS_LOADER.getResource(dir + filename);
			if (url != null) {
				break;
			} else {
				System.err.println(dir + filename + " not found!");
			}
		}

		return url;
	}

	public static BufferedImage getImage(String filename) {
		BufferedImage bi = null;

		try {
			bi = ImageIO.read(getResource(filename));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bi;
	}

	public static BufferedImage getSpriteImage(SpriteType type, String spriteKey,
			String filename) {
		BufferedImage bi = null;

		String file = type.toString().toLowerCase() + "/" + spriteKey + "/"
				+ filename;

		try {
			bi = ImageIO.read(getResource(file));
		} catch (Exception e) {
			System.err.println("file not found: " + file);
			e.printStackTrace();
		}

		return bi;
	}
}
